package leetcode.L200;

class Solution {
    public int numIslands(char[][] grid) {
        int sumIsands=0;
        if (grid == null || grid.length == 0) {
            return 0;
        }
        int gridRow = grid.length;
        int gridColumn = grid[0].length;
        for (int i = 0; i < gridRow; i++) {
            for (int j = 0; j < gridColumn; j++) {
                if (grid[i][j] == '1') {
                    sumIsands++;
                }
                dfs(grid, i, j);
            }
        }
        return sumIsands;
    }

    public void dfs(char[][] grid,int i, int j) {
        //先判断是否超出长度
        int gridRow = grid.length;
        int gridColumn = grid[0].length;
        if (i < 0 || i >= gridRow || j < 0 || j >= gridColumn|| grid[i][j]=='0' ) {//判断是否==0应该放最后
            return;
        }
        grid[i][j] = '0';
        //设置其上下左右为0
        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
        dfs(grid,i,j+1);
        dfs(grid,i,j-1);
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.numIslands(new char[][]{{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}});
    }
}